package com.heima.search.mq;

import com.heima.search.constants.MyMqConstants;
import com.heima.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class MyMqItemListener {
    @Autowired
    private SearchService searchService;

    /**
     * 新增和修改的es 操作
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MyMqConstants.ITEM_INSERT_QUEUE),
            exchange = @Exchange(name = MyMqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = {MyMqConstants.ITEM_INSERT_KEY}
    ))
    public void myMqItemListenerInsertOrUpdate(Long id) {
        log.info("MQ 调用新增和修改的es 操作id={}", id);
        searchService.myMqItemListenerInsertOrUpdate(id);
    }

    /**
     * 删除的es
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MyMqConstants.ITEM_DELETE_QUEUE),
            exchange = @Exchange(name = MyMqConstants.ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = {MyMqConstants.ITEM_DELETE_KEY}
    ))
    public void myMQItemListenerDelete(Long id) {
        log.info("MQ 调用删除 的es 操作id={}", id);
        searchService.myMQItemListenerDelete(id);
    }
}
